package org.kev.transit.worker;

import com.google.android.maps.Overlay;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RadialGradient;
import android.graphics.Rect;

import android.graphics.Paint.Cap;
import android.graphics.Shader.TileMode;
import android.util.Log;
import com.google.android.maps.Point;

/**
 * Location overlay displays queried station and current GPS location on a map
 * 
 * @author kevin
 * 
 */
public class LocOverlay extends Overlay {
	private static final String TAG = "LocOverlay";
	// BrowseMap mMap;
	private static Paint paint1 = null;
	private static Paint paint2 = null;
	private static RadialGradient shader =null;
	
	
	// Paint paint2 = new Paint();
	static Point stationPoint = null;
	static Point currentPoint = null;

	public LocOverlay(Point station, Point current) {
		stationPoint = station;
		currentPoint = current;
		if(shader==null)
			shader = new RadialGradient(2, 2, 2, Color.LTGRAY, Color.BLACK, TileMode.CLAMP);
		if(paint1==null){
			paint1=new Paint();
			paint1.setShader(shader);
		}	
		paint2=new Paint();
		paint2.setColor(Color.RED);
		// paint2.setARGB(255, 255, 255, 255);
	}

	/**
	 * 
	 * @see com.google.android.maps.Overlay#draw(android.graphics.Canvas, com.google.android.maps.Overlay.PixelCalculator, boolean)
	 */
	@Override
	public void draw(Canvas canvas, PixelCalculator pixelCalculator, boolean b) {
		super.draw(canvas, pixelCalculator, b);

		int[] screenCoords = new int[2];
		//float[] triangle=new float[6];

		
		

		pixelCalculator.getPointXY(currentPoint, screenCoords);

		
		canvas.drawCircle(screenCoords[0], screenCoords[1], 6, paint1);
		 canvas.drawText("You are here",
				 screenCoords[0] - 35,
				 screenCoords[1] + -10, paint2);
		pixelCalculator.getPointXY(stationPoint, screenCoords);
		Rect r = new Rect(screenCoords[0] - 5, screenCoords[1] - 5,
				screenCoords[0] + 5, screenCoords[1] + 5);
		
		canvas.drawRect(r, paint1);


	}

	/**
	 *  return span number of degrees Latitude between two points
	 * @see com.google.android.maps.Overlay#getLatSpanE6()
	 */
	@Override
	public int getLatSpanE6() {
		int lat1 = currentPoint.getLatitudeE6();
		int lat2 = stationPoint.getLatitudeE6();
		int total = -1;
		if ((lat1 < 0 && lat2 < 0) || (lat1 > 0 && lat2 > 0)) { // both are same
																// sign
			total = Math.abs(lat1) - Math.abs(lat2);
			total = Math.abs(total); // drop any sign
		} else { // signs are different
			total = Math.abs(lat1) + Math.abs(lat2);
			total = Math.abs(total); // drop any sign
		}

		return total;
	}

	/** 
	 * 
	 * return number of degrees longitude between two points
	 * @see com.google.android.maps.Overlay#getLonSpanE6()
	 */
	@Override
	public int getLonSpanE6() {
		int lon1 = currentPoint.getLongitudeE6();
		int lon2 = stationPoint.getLongitudeE6();
		int total = -1;
		if ((lon1 < 0 && lon2 < 0) || (lon1 > 0 && lon2 > 0)) { // both are same
																// sign
			// Log.d(TAG,"lon both hav same sign");
			total = Math.abs(lon1) - Math.abs(lon2);
			Math.abs(total); // drop any sign
			// Log.d(TAG,"lon diff from 0 is n"+total);

		} else { // signs are different
			total = Math.abs(lon1) + Math.abs(lon2);
			Math.abs(total); // drop any sign
		}
		return total+20; //sometimes the map is where my points do now show up
	}

	/**
	 * suggest a new center point that is between the two mapped location
	 */
	@Override
	public Point getCenter() {
		Point p = super.getCenter();
		int lon1 = currentPoint.getLongitudeE6();
		int lon2 = stationPoint.getLongitudeE6();
		int lat1 = currentPoint.getLatitudeE6();
		int lat2 = stationPoint.getLatitudeE6();
		int alon = (lon1 + lon2) / 2;
		int alat = (lat1 + lat2) / 2;

		//Log.d(TAG, "suggest center at lat=" + alat + " lon=" + alon);

		p = new Point(alat, alon);

		return p;
	}

}
